package cn.tedu.vr;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.vr.sdk.widgets.video.VrVideoEventListener;
import com.google.vr.sdk.widgets.video.VrVideoView;

public class VRVideoActivity extends AppCompatActivity {
    //全景视频控件
    private VrVideoView videoView;
    //异步任务对象
    private AsyncTask<Void,Void,Void> task;
    //显示视频百分比进度
    private TextView seekbarText;
    //进度条
    private SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vrvideo);
        init();
    }

    private void init() {
        seekBar = (SeekBar)findViewById(R.id.seekbar);
        seekbarText = (TextView)findViewById(R.id.seekbar_text);
        //进度条播放监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            //进度变更
            //seekBar – 进度发生变化的 SeekBar
            //progress - 当前进度级别。 这将在 min..max 范围内，
            // 其中 min 和 max 分别由ProgressBar.setMin(int)和ProgressBar.setMax(int) 。
            // （最小值的默认值为 0，最大值为 100。）
            //fromUser – 如果进度更改是由用户发起的，则为 True
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser){
                    //获取总进度
                    long duration = videoView.getDuration();
                    //当前进度，百分比
                    long newPosition = (long)(progress * 0.01f * duration);
                    videoView.seekTo(newPosition);
                }
            }
            //开始
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            //停止
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        videoView = (VrVideoView)findViewById(R.id.vr_video);
        //setTag()设置与此视图关联的标签
        videoView.setTag(true);
        //监听视频的暂停与播放
        videoView.setEventListener(new VrVideoEventListener(){
            @Override
            public void onClick() {
                super.onClick();
                //获取播放状态
                boolean isPlay = (boolean)videoView.getTag();
                if (isPlay){
                    isPlay = false;
                    //暂停播放
                    videoView.pauseVideo();
                } else {
                    isPlay = true;
                    //继续播放
                    videoView.playVideo();
                }
                videoView.setTag(isPlay);
            }

            //视频画面切换到下一帧时被调用
            @Override
            public void onNewFrame() {
                super.onNewFrame();
                //设置进度条的最大值上限
                seekBar.setMax(100);
                //获取视频时长，单位为毫秒
                long duration = videoView.getDuration();
                //获取当前位置
                long currentPosition = videoView.getCurrentPosition();
                //float转换为int会丢失精度，为了减少误差+0.5f
                int percent = (int)(currentPosition *100f / duration + 0.5f);
                //进度条设置位置
                seekBar.setProgress(percent);
                seekbarText.setText(percent + "%");
            }
            //加载成功，设置进度为0，最大值为100
            @Override
            public void onLoadSuccess() {
                super.onLoadSuccess();
                Toast.makeText(VRVideoActivity.this, "开始播放", Toast.LENGTH_SHORT)
                        .show();
                seekBar.setMax(100);
                seekBar.setProgress(0);
                seekbarText.setText("0%");
            }
            //加载视频失败
            @Override
            public void onLoadError(String errorMessage) {
                super.onLoadError(errorMessage);
                Toast.makeText(VRVideoActivity.this, "播放出错", Toast.LENGTH_SHORT)
                        .show();
            }
            //播放完成
            @Override
            public void onCompletion() {
                super.onCompletion();
                seekBar.setMax(100);
                seekBar.setProgress(100);
                seekbarText.setText("100%");
            }
        });
        //创建异步任务
        task = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                String fileName = "congo.mp4";
                VrVideoView.Options options = new VrVideoView.Options();
                //非流媒体
                options.inputFormat = VrVideoView.Options.FORMAT_DEFAULT;
                //设置3D效果
                options.inputType = VrVideoView.Options.TYPE_STEREO_OVER_UNDER;
                try {
                    //从资源目录加载视频
                    videoView.loadVideoFromAsset(fileName,options);
                } catch (Exception e){

                }
                return null;
            }

            @Override
            protected void onPostExecute(Void unused) {
                super.onPostExecute(unused);
                Toast.makeText(VRVideoActivity.this,"加载成功，开始播放",Toast.LENGTH_SHORT).show();
            }
        }.execute();
    }

    //细节一.切换模式造成屏幕变黑
    @Override
    protected void onResume() {
        super.onResume();
        videoView.resumeRendering();//重新渲染
    }

    @Override
    protected void onPause() {
        super.onPause();
        videoView.pauseRendering();//停止渲染
    }

    //细节二.AsyncTask处理
    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.shutdown();//停止
        if (task != null) {
            task.cancel(true);
            task = null;
        }
    }
}